package com.dqhl.community.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dqhl.community.R;
import com.dqhl.community.base.BaseActivity;
import com.dqhl.community.util.Config;
import com.dqhl.community.util.Constant;
import com.dqhl.community.util.Dlog;
import com.dqhl.community.util.FormatUtils;
import com.dqhl.community.util.JsonUtils;
import com.dqhl.community.util.TimeCountUtil;
import com.dqhl.community.util.UserSaveUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by cheng on 2016/8/30.
 * 修改手机号码
 */
public class ModifyPhoneActivity extends BaseActivity implements View.OnClickListener {

    private ImageView ivTopBack;
    private TextView tvTopTitle;
    private EditText et_phone;
    private EditText et_code;

    private Button btnGetCode;
    private Button btnSubmit;
    private long millisInFuture = Constant.TIMECOUNT_MODIFY_PHONE; //验证码倒计时

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_phone);

        initView();
    }

    private void initView() {
        ivTopBack = (ImageView) findViewById(R.id.iv_top_back);
        ivTopBack.setVisibility(View.VISIBLE);
        tvTopTitle = (TextView) findViewById(R.id.tv_top_title);
        tvTopTitle.setText(R.string.txt_personal_setting);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_code = (EditText) findViewById(R.id.et_code);

        btnGetCode = (Button) findViewById(R.id.btn_get_code);
        btnSubmit = (Button) findViewById(R.id.btn_submit);

        ivTopBack.setOnClickListener(this);
        btnGetCode.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
            case R.id.btn_get_code: // 获取验证码
                doGetCode();
                break;
            case R.id.btn_submit: //提交
                doSubmit();
                break;
        }
    }

    /**
     * 提交结果
     */
    private void doSubmit() {
        final String strPhone = et_phone.getText().toString();
        String code = et_code.getText().toString();
        if (!FormatUtils.isMobileNO(strPhone)) {
            toast("手机格式不正确");
        } else if (TextUtils.isEmpty(code)) {
            toast("请输入验证码");
        } else {
            Dlog.e(user.getUserid() + ";" + strPhone + ";" + code);
            RequestParams params = new RequestParams(Config.Api.modify_phone);
            params.addBodyParameter("userid", user.getUserid());
            params.addBodyParameter("phone_new", strPhone);
            params.addBodyParameter("code", code);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    if (0 == errCode) {
                        spUserInfo.getString(Constant.USER_PHONE, "");
                        spUserInfo.edit().putString(Constant.USER_PHONE, strPhone).apply();
                        user.setPhone(strPhone);
                        UserSaveUtil.saveUser(ModifyPhoneActivity.this, user);
                        Intent intent = new Intent(Constant.ACTION_MODIFY_PHONE_SUCCESS);
                        intent.putExtra(Constant.USER_PHONE, strPhone);
                        sendBroadcast(intent);
                        finish();
                    }
                    toast(errMsg);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }

    /**
     * 获取验证码
     */
    private void doGetCode() {
        String strPhone = et_phone.getText().toString();
        if (!FormatUtils.isMobileNO(strPhone)) {
            toast("手机格式不正确");
        } else {
            TimeCountUtil timeCountUtil = new TimeCountUtil(
                    ModifyPhoneActivity.this, millisInFuture, 1000, btnGetCode);
            timeCountUtil.start();
            Dlog.e(strPhone);
            RequestParams params = new RequestParams(Config.Api.modify_phone_get_code);
            params.addBodyParameter("phone", strPhone);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e("result:" + result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    if (errCode == 0) {
                        Dlog.e(errMsg);
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }
    }


}
